﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using NDF.XnaLibrary;

namespace NDF.ConnectionLibrary
{
	public class PlayerInfo
	{
		/// <summary>
		/// Gets the player`s ID.
		/// </summary>
		public int PlayerID { get; private set; }

		/// <summary>
		/// Gets the TcpClient used for sending information to the player.
		/// </summary>
		public TcpClient SendingClient { get; private set; }

		/// <summary>
		/// Gets the stream used for sending information to the player.
		/// </summary>
		public NetworkStream SendingStream { get; private set; }

		/// <summary>
		/// Gets the TcpClient used for receiving information from the player.
		/// </summary>
		public TcpClient ReceivingClient { get; private set; }

		/// <summary>
		/// Gets the stream used for receiving information to the player.
		/// </summary>
		public NetworkStream ReceivingStream { get;	private set; }

		/// <summary>
		/// Initializes a new PlayerInfo object with the given parameters.
		/// </summary>
		/// <param name="playerID">The player`s ID.</param>
		/// <param name="sendingClient">The TcpClient of the player.</param>
		public PlayerInfo(int playerID, TcpClient sendingClient, TcpClient receivingClient)
		{
			if (sendingClient == null)
				throw new ArgumentNullException("sendingClient", "Client cannot be null");
			if (!sendingClient.Connected)
				throw new ArgumentException("The sendingClient must have already connected to the server", "sendingClient");
			if (!receivingClient.Connected)
				throw new ArgumentException("The receivingClient must have already connected to the server", "receivingClient");

			PlayerID = playerID;
			SendingClient = sendingClient;
			SendingStream = sendingClient.GetStream();
			ReceivingClient = receivingClient;
			ReceivingStream = receivingClient.GetStream();
		}

		public override string ToString()
		{
			return string.Format("ID: {0}, Address: {1}", PlayerID, ReceivingClient.Client.RemoteEndPoint);
		}

		/// <summary>
		/// Releases all resources used by the current instance.
		/// </summary>
		public void Close()
		{
			SendingClient.Close();
			ReceivingClient.Close();
			SendingStream.Close();
			ReceivingStream.Close();
		}
	}
}
